package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.service.AskAnswerItemService;
import com.api.shopapi.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * 问卷调查项
 * */

@RestController
@RequestMapping("AskAnswerItem")
public class AskAnswerItemController extends BaseController{


    @Autowired
    @Qualifier("AskAnswerItemServiceImplOne")
    private AskAnswerItemService askAnswerItemService;


    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;



    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Insert(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String name = jsonObject.getString("name");
            String askAnswerGroupCode = jsonObject.getString("askAnswerGroupCode");

            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerItem/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            AskAnswerItem askAnswerItem = new AskAnswerItem();
            askAnswerItem.setAskAnswerItemCode(UUID.randomUUID().toString());
            askAnswerItem.setAskAnswerGroupCode(askAnswerGroupCode);
            askAnswerItem.setName(name);
            askAnswerItemService.insert(askAnswerItem);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerItem/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String name = jsonObject.getString("name");
            String askAnswerGroupCode = jsonObject.getString("askAnswerGroupCode");

            String staffCode = jsonObject.getString("staffCode");

            String askAnswerItemCode = jsonObject.getString("askAnswerItemCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerItem/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            AskAnswerItem askAnswerItem = askAnswerItemService.getByAskAnswerItemCode(askAnswerItemCode);
            askAnswerItem.setName(name);
            askAnswerItem.setAskAnswerGroupCode(askAnswerGroupCode);
            askAnswerItemService.update(askAnswerItem);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerItem/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult getList(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String askAnswerGroupCode = jsonObject.getString("askAnswerGroupCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerItem/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            List<AskAnswerItem> list = askAnswerItemService.ListByAskAnswerGroupCode(askAnswerGroupCode);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("list",list);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerItem/List",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }



}
